import express from 'express';
import cors from 'cors';
import swaggerUi from 'swagger-ui-express';
import { swaggerDocument } from './config/swagger';
import userRoutes from './routes/user.routes';
import courseRoutes from './routes/course.routes';
import teacherRoutes from './routes/teacher.routes';

const app = express();
const port = 3000;

// 配置 CORS
app.use(cors({
  origin: ['http://localhost:5173', 'http://127.0.0.1:5173'],
  credentials: true
}));

app.use(express.json());

// 添加CORS中间件
app.use(cors());

// 配置Swagger文档路由
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

// 添加用户路由
app.use('/api/user', userRoutes);

// 添加课程路由
app.use('/api/courses', courseRoutes);

// 添加讲师路由
app.use('/api/teachers', teacherRoutes);

// 基础路由
app.get('/', (req, res) => {
  res.send('讲课APP后端服务运行中...');
});

app.listen(port, () => {
  console.log(`服务器运行在 http://localhost:${port}`);
  console.log(`API文档地址: http://localhost:${port}/api-docs`);
});
